﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Xml;
using System.ComponentModel;

namespace BoAddonBiz_FreightForwarder.Custom
{
    internal class CheckXmlFile : INotifyPropertyChanged
    {
        public CheckXmlFile()
        {
            _result = new Result();
        }
        public string TaskId { get; set; }
        private bool _isSendResult = false;

        public bool isSendResult
        {
            get { return _isSendResult; }
            private set
            {
                _isSendResult = value;
                OnPropertyChanged("isSendResult");
            }
        }
        private bool _isReceiveResult1 = false;

        public bool isReceiveResult1
        {
            get { return _isReceiveResult1; }
            private set
            {
                _isReceiveResult1 = value;
                OnPropertyChanged("isReceiveResult1");
            }
        }

        private bool _isReceiveResult2 = false;
        public bool isReceiveResult2
        {
            get { return _isReceiveResult2; }
            private set
            {
                _isReceiveResult2 = value;
                OnPropertyChanged("isReceiveResult2");
            }
        }

        private Result _result;
        public Result result
        {
            get { return _result; }
        }


        public void Check()
        {
            SAPBoAddon.B1Addon.B1Addon.SBOApp.SetStatusBarMessage("开始遍历文件夹……", SAPbouiCOM.BoMessageTime.bmt_Long, false);
            while (isSendResult == false || isReceiveResult1 == false || isReceiveResult2 == false)
            {
                #region sendresult
                if (isSendResult == false)
                {
                    string SendResult = ConfigurationManager.AppSettings["SendResult"];
                    foreach (string file in Directory.GetFiles(SendResult))
                    {
                        XmlDocument xml = new XmlDocument();
                        xml.Load(file);
                        XmlNamespaceManager xmlmanager = new XmlNamespaceManager(xml.NameTable);
                        xmlmanager.AddNamespace("sn", "http://www.w3.org/2001/XMLSchema-instance");
                        var path = xml.SelectSingleNode("//root/处理结果文字信息", xmlmanager).InnerText;
                        var start = path.LastIndexOf("\\");
                        var end = path.LastIndexOf(".");
                        var rTaskid = path.Substring(start + 1, end - start - 1);
                        if (rTaskid == TaskId)
                        {
                            int s = file.LastIndexOf("\\");
                            string newPath = file.Remove(s, 1).Insert(s, "-Backup\\");
                            File.Move(file, newPath);
                            isSendResult = true;
                            break;
                        }
                    }
                }
                #endregion
                #region receiveresult
                string ReceiveResult = ConfigurationManager.AppSettings["ReceiveResult"];
                if (isReceiveResult1 == false || isReceiveResult2 == false)
                {
                    foreach (var file in Directory.GetFiles(ReceiveResult))
                    {
                        XmlDocument xml = new XmlDocument();
                        xml.Load(file);
                        XmlNamespaceManager xmlmanager = new XmlNamespaceManager(xml.NameTable);
                        xmlmanager.AddNamespace("cp", "http://www.chinaport.gov.cn/tcs/v2");
                        var messageType = xml.SelectSingleNode("//TCS101Message/MessageHead/MessageType").InnerText;
                        if (messageType == "TcsFlow201")
                        {
                            var rTaskId1 = xml.SelectSingleNode("//cp:TaskId", xmlmanager).InnerText;
                            if (rTaskId1 == TaskId)
                            {
                                xmlmanager = new XmlNamespaceManager(xml.NameTable);
                                xmlmanager.AddNamespace("cp", "http://www.chinaport.gov.cn/tcs/v2");
                                //Channel
                                var resultCode = xml.SelectSingleNode("//cp:TcsData/cp:ENTRY_RESULT/cp:ResultInformation", xmlmanager).InnerText;
                                xmlmanager = new XmlNamespaceManager(xml.NameTable);
                                xmlmanager.AddNamespace("cp", "http://www.chinaport.gov.cn/tcs/v2");
                                var resultValue1 = xml.SelectSingleNode("//cp:TcsData/cp:ENTRY_RESULT/cp:ResultInformation", xmlmanager).InnerText;
                                if (resultCode == "001")
                                {
                                    result.flag = true;
                                }
                                else
                                    result.flag = false;
                                result.message += resultValue1;
                                int s = file.LastIndexOf("\\");
                                string newPath = file.Remove(s, 1).Insert(s, "-Backup\\");
                                File.Move(file, newPath);
                                result.message = resultValue1 + "\r\n";
                                isReceiveResult1 = true;
                            }
                        }
                        if (messageType == "TcsFlow201Response")
                        {
                            var rTaskId2 = xml.SelectSingleNode("//cp:TaskId", xmlmanager).InnerText;
                            if (rTaskId2 == TaskId)
                            {
                                xmlmanager = new XmlNamespaceManager(xml.NameTable);
                                xmlmanager.AddNamespace("cp", "http://www.chinaport.gov.cn/tcs/v2");
                                var resultCode2 = xml.SelectSingleNode("//cp:ActionResult/cp:ResultCode", xmlmanager).InnerText;
                                var resultValue2 = xml.SelectSingleNode("//cp:ActionResult/cp:ResultValue", xmlmanager).InnerText;
                                int s = file.LastIndexOf("\\");
                                string newPath = file.Remove(s, 1).Insert(s, "-Backup\\");
                                File.Move(file, newPath);
                                isReceiveResult2 = true;
                            }
                        }
                    }
                }
                #endregion
            }
        }

        internal class Result
        {
            public bool flag { get; set; }
            public string message { get; set; }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}
